using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ItemType
/// </summary>
public class ItemTypeDAO : DBConnection
{
    #region sql command
    string SQL_SELECT_ID = "";
    string SQL_SELECT_ALL = "";
    string SQL_INSERT = "insert into ItemType(TypeName, Description) values(@typeName, @description)";
    string SQL_UPDATE = "update ItemType set TypeName = @typeName, Description = @description where ItemTypeID = @itemTypeId";
    string SQL_DELETE = "delete from ItemType where ItemTypeID = @itemTypeId";
    #endregion

    #region fields
    string message = "";

    
    #endregion

    #region constructors
    public ItemTypeDAO()
    {
    }
    #endregion

    #region properties
    public string Message
    {
        get { return message; }
    }
    #endregion

    #region methods
    public int InsertItemType(ItemType itemType)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_INSERT, conn);
            cmd.Parameters.AddWithValue("@typeName", itemType.Name);
            cmd.Parameters.AddWithValue("@description", itemType.Description);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int UpdateItemType(ItemType itemType)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_UPDATE, conn);
            cmd.Parameters.AddWithValue("@itemTypeId", itemType.ItemTypeID);
            cmd.Parameters.AddWithValue("@typeName", itemType.Name);
            cmd.Parameters.AddWithValue("@description", itemType.Description);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;       
    }

    public int DeleteItemType(ItemType itemType)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_DELETE, conn);
            cmd.Parameters.AddWithValue("@itemTypeId", itemType.ItemTypeID);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }
    #endregion
}
